package platform_api.Service;

import org.springframework.stereotype.Service;
import platform_api.Model.CityInfo;
import platform_api.Model.JsonMessage;
import platform_api.Model.WeatherData;
import platform_api.Repository.CityInfoRepository;
import platform_api.Repository.WeatherDataRepository;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class WeatherService extends BaseService {

    private final WeatherDataRepository weatherDataRepository;
    private CityInfoRepository cityInfoRepository;

    public WeatherService(WeatherDataRepository weatherDataRepository, CityInfoRepository cityInfoRepository) {
        this.weatherDataRepository = weatherDataRepository;
        this.cityInfoRepository = cityInfoRepository;
    }

    /**
     * 获取最小值和最大值
     *
     * @param city      城市id
     * @param column    字段
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return Object 最小值
     */
    private Object getMinValue(String city, String column, Date startTime, Date endTime) {
        Object value = weatherDataRepository.getMinValue(Integer.valueOf(city), column, startTime, endTime);
        if (value == null) {
            value = 0;
        }
        return value;
    }

    private Object getMaxValue(String city, String column, Date startTime, Date endTime) {
        Object value = weatherDataRepository.getMaxValue(Integer.valueOf(city), column, startTime, endTime);
        if (value == null) {
            value = 0;
        }
        return value;
    }

    /**
     * 获取echarts表格所需数据
     *
     * @param city      城市id
     * @param column    字段名
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @param distance  粒度
     * @return JsonMessage 数据
     */
    public JsonMessage getChartData(String city, String column, Date startTime, Date endTime, String distance) {
        Date timeIterator = startTime;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startTime);
        int field; // 查询粒度

        switch (distance) {
            case "day":
                field = Calendar.DATE;
                break;
            case "month":
                field = Calendar.MONTH;
                break;
            default:
                return getWeatherDataOfEachHour(city, column, startTime, endTime);
        }

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

        ArrayList<String> category = new ArrayList<>();
        ArrayList<String> info = new ArrayList<>();
        while (timeIterator.compareTo(endTime) < 0) {
            Date tmp = timeIterator;
            calendar.add(field, 1);
            timeIterator = calendar.getTime();

            Object mean = weatherDataRepository.getMeanOfSingleColumn(Integer.valueOf(city), column, tmp, timeIterator);

            if (mean == null) {
                mean = 0;
            }

            if (mean instanceof Double) {
                mean = String.format("%.2f", mean);
            }

            System.out.println(df.format(tmp));
            System.out.println(df.format(timeIterator));
            category.add(df.format(tmp));
            info.add(mean.toString());

        }
        return getJsonMessage(category, info, getMinValue(city, column, startTime, endTime), getMaxValue(city, column, startTime, endTime), WeatherData.getColumnName(column));
    }

    /**
     * 获取粒度为小时的数据
     *
     * @param city      城市id
     * @param column    字段
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return JsonMessage 数据
     */
    private JsonMessage getWeatherDataOfEachHour(String city, String column, Date startTime, Date endTime) {
        ArrayList<String> category = new ArrayList<>();
        ArrayList<String> data = new ArrayList<>();
        List<WeatherData> weatherData = weatherDataRepository.getByCityIdAndObservationTime(startTime, endTime, Integer.parseInt(city));
        for (WeatherData item : weatherData) {
            category.add(item.getObservationTime().toString());
            data.add(item.getColumn(column));
        }
        return getJsonMessage(category, data, getMinValue(city, column, startTime, endTime), getMaxValue(city, column, startTime, endTime), WeatherData.getColumnName(column));
    }

    /**
     * 获取城市列表
     *
     * @return JsonMessage
     */
    public JsonMessage getCityInfo() {
        List<CityInfo> cityInfoList = cityInfoRepository.getAllCityInfo();
        Map<String, List<CityInfo>> data = new HashMap<>();
        data.put("data", cityInfoList);
        return new JsonMessage(200, data);
    }

}
